<template>
  <div style="display:inline-block;font-size:17px;font-weight:700">
    当前时间:{{ nowTime }}
  </div>
</template>
<script>
export default {
  name: 'NowTime',
  data () {
    return {
      nowTime: ''
    }
  },
  mounted () {
    this.getNowTime()
  },
  methods: {
    getNowTime () {
      const speed = 1000
      const that = this
      const theNowTime = function () {
        that.nowTime = that.timeNumber()
      }
      setInterval(theNowTime, speed)
    },
    timeNumber () {
      const today = new Date()
      const date = today.getFullYear() + '-' + this.twoDigits(today.getMonth() + 1) + '-' + this.twoDigits(today.getDate())
      const time = this.twoDigits(today.getHours()) + ':' + this.twoDigits(today.getMinutes()) + ':' + this.twoDigits(today.getSeconds())
      return date + '  ' + time
    },
    twoDigits (val) {
      if (val < 10) return '0' + val
      return val
    }
  }
}
</script>
